//
//  ZDRefreshView.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/11/5.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDRefreshView.h"

@interface ZDRefreshView ()

@property (weak, nonatomic) IBOutlet UIImageView *arrowView;

@property (weak, nonatomic) IBOutlet UIView *tipView;

@property (weak, nonatomic) IBOutlet UIImageView *loadingView;

@end

@implementation ZDRefreshView


+ (instancetype)refreshView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}


- (void)rotateArrow:(BOOL)flag
{
    [UIView animateWithDuration:0.25 animations:^{
        
        self.arrowView.transform = flag ? CGAffineTransformMakeRotation(M_PI-0.01) : CGAffineTransformIdentity;
    }];
}

- (void)startRefreshing
{
    //隐藏tipView
    self.tipView.hidden = YES;
    
    //如果动画正在执行
    if ([self.loadingView.layer animationForKey:@"anima"]) {
        
        return;
    }
    
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    anima.toValue = @(M_PI * 2);
    anima.duration = 0.25;
    anima.repeatCount = MAXFLOAT;
    
    //设置动画停止后不移除 否则因控制器切换 会终止动画
    anima.removedOnCompletion = NO;
    
    //添加动画
    [self.loadingView.layer addAnimation:anima forKey:@"anima"];
}

- (void)stopRefreshing
{
    self.tipView.hidden = NO;
    
    //移除动画
    [self.loadingView.layer removeAllAnimations];
}

@end
